package com.wang.gallerybackend.controller;

import com.wang.gallerybackend.common.AjaxResult;
import com.wang.gallerybackend.common.BaseResponse;
import com.wang.gallerybackend.exception.ErrorCode;
import com.wang.gallerybackend.exception.ThrowUtils;
import com.wang.gallerybackend.model.dto.space.analyze.*;
import com.wang.gallerybackend.model.entity.Space;
import com.wang.gallerybackend.model.entity.User;
import com.wang.gallerybackend.model.vo.space.SpaceUserAnalyzeResponse;
import com.wang.gallerybackend.model.vo.space.analyze.SpaceCategoryAnalyzeResponse;
import com.wang.gallerybackend.model.vo.space.analyze.SpaceSizeAnalyzeResponse;
import com.wang.gallerybackend.model.vo.space.analyze.SpaceTagAnalyzeResponse;
import com.wang.gallerybackend.model.vo.space.analyze.SpaceUsageAnalyzeResponse;
import com.wang.gallerybackend.service.SpaceAnalyzeService;
import com.wang.gallerybackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/spaceAnalyze")
@Slf4j
public class SpaceAnalyzeController {
	@Resource
	private SpaceAnalyzeService spaceAnalyzeService;
	@Resource
	private UserService userService;

	/**
	 * 空间使用情况
	 * @param spaceUsageAnalyzeRequest
	 * @param request
	 * @return
	 */
	@PostMapping("/usage")
	public BaseResponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(
			SpaceUsageAnalyzeRequest spaceUsageAnalyzeRequest
			, HttpServletRequest request) {
		ThrowUtils.throwIf(spaceUsageAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
		User loginUser = userService.getLoginUser(request);
		return AjaxResult.success(spaceAnalyzeService.spaceUsageAnalyze(spaceUsageAnalyzeRequest, loginUser));
	}

	/**
	 * 分类空间使用情况
	 * @param spaceCategoryAnalyzeRequest
	 * @param request
	 * @return
	 */
	@PostMapping("/category")
	public BaseResponse<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyze(
			SpaceCategoryAnalyzeRequest spaceCategoryAnalyzeRequest
			, HttpServletRequest request
	) {
		ThrowUtils.throwIf(spaceCategoryAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
		User loginUser = userService.getLoginUser(request);
		return AjaxResult.success(spaceAnalyzeService.getSpaceCategoryAnalyze(spaceCategoryAnalyzeRequest, loginUser));
	}

	/**
	 * 标签空间使用情况
	 * @param spaceTagAnalyzeRequest
	 * @param request
	 * @return
	 */
	@PostMapping("/tag")
	 public BaseResponse<List<SpaceTagAnalyzeResponse>> getSpaceTagAnalyze(SpaceTagAnalyzeRequest spaceTagAnalyzeRequest, HttpServletRequest request) {
		ThrowUtils.throwIf(spaceTagAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
		User loginUser = userService.getLoginUser(request);
		return AjaxResult.success(spaceAnalyzeService.getSpaceTagAnalyze(spaceTagAnalyzeRequest, loginUser));
	}

	/**
	 *  大小空间使用情况
	 * @param spaceSizeAnalyzeRequest
	 * @param request
	 * @return
	 */
	@PostMapping("/size")
	public BaseResponse<List<SpaceSizeAnalyzeResponse>> getSpaceSizeAnalyze(SpaceSizeAnalyzeRequest spaceSizeAnalyzeRequest, HttpServletRequest request) {
		ThrowUtils.throwIf(spaceSizeAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
		User loginUser = userService.getLoginUser(request);
		return AjaxResult.success(spaceAnalyzeService.getSpaceSizeAnalyze(spaceSizeAnalyzeRequest, loginUser));
	}

	/**
	 * 用户空间使用情况
	 * @param spaceUserAnalyzeRequest
	 * @param request
	 * @return
	 */
	@PostMapping("/user")
	public BaseResponse<List<SpaceUserAnalyzeResponse>> getSpaceUserAnalyze(SpaceUserAnalyzeRequest spaceUserAnalyzeRequest, HttpServletRequest request) {
		ThrowUtils.throwIf(spaceUserAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
		User loginUser = userService.getLoginUser(request);
		return AjaxResult.success(spaceAnalyzeService.getSpaceUserAnalyze(spaceUserAnalyzeRequest, loginUser));
	}


	/**
	 * 空间排行
	 * @param spaceRankAnalyzeRequest
	 * @param request
	 * @return
	 */
	@PostMapping("/rank")
	public BaseResponse<List<Space>> getSpaceRankAnalyze(SpaceRankAnalyzeRequest spaceRankAnalyzeRequest, HttpServletRequest request) {
		ThrowUtils.throwIf(spaceRankAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
		User loginUser = userService.getLoginUser(request);
		return AjaxResult.success(spaceAnalyzeService.getSpaceRankAnalyze(spaceRankAnalyzeRequest, loginUser));
	}
}
